package com.dabai.iot.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dabai.common.core.utils.Func;
import com.dabai.iot.core.convert.AlarmRecordConvert;
import com.dabai.iot.core.dto.AlarmRecordQuery;
import com.dabai.iot.core.entity.AlarmEntity;
import com.dabai.iot.core.entity.AlarmRecordEntity;
import com.dabai.iot.core.entity.DeviceEntity;
import com.dabai.iot.core.entity.ProductEntity;
import com.dabai.iot.core.mapper.AlarmRecordMapper;
import com.dabai.iot.core.service.IAlarmRecordService;
import com.dabai.iot.core.service.IAlarmService;
import com.dabai.iot.core.service.IDeviceService;
import com.dabai.iot.core.service.IProductService;
import com.dabai.iot.core.vo.AlarmRecordVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 告警记录 Service 实现类
 *
 * @author kev1n
 */
@Service
@RequiredArgsConstructor
public class AlarmRecordServiceImpl extends ServiceImpl<AlarmRecordMapper, AlarmRecordEntity> implements IAlarmRecordService {

    private final IAlarmService alarmService;
    private final IProductService productService;
    private final IDeviceService deviceService;

    @Override
    public IPage<AlarmRecordVO> getRecordPage(IPage<AlarmRecordEntity> page, AlarmRecordQuery recordQuery) {
        QueryWrapper<AlarmRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
            .eq(Func.isNotEmpty(recordQuery.getAlarmId()), AlarmRecordEntity::getAlarmId, recordQuery.getAlarmId())
            .eq(Func.isNotEmpty(recordQuery.getProductId()), AlarmRecordEntity::getProductId, recordQuery.getProductId())
            .eq(Func.isNotEmpty(recordQuery.getDeviceId()), AlarmRecordEntity::getDeviceId, recordQuery.getDeviceId())
            .eq(Func.isNotEmpty(recordQuery.getTraceId()), AlarmRecordEntity::getTraceId, recordQuery.getTraceId())
            .ge(Func.isNotEmpty(recordQuery.getQueryStartTime()), AlarmRecordEntity::getCreateTime, recordQuery.getQueryStartTime())
            .le(Func.isNotEmpty(recordQuery.getQueryEndTime()), AlarmRecordEntity::getCreateTime, recordQuery.getQueryEndTime())
            .eq(AlarmRecordEntity::getDelFlag, "0")
            .orderByDesc(AlarmRecordEntity::getCreateTime);

        IPage<AlarmRecordEntity> result = page(page, queryWrapper);
        return result.convert(this::resolveRecordVO);
    }

    private AlarmRecordVO resolveRecordVO(AlarmRecordEntity alarmRecord) {
        if (alarmRecord != null) {
            AlarmRecordVO vo = AlarmRecordConvert.INSTANCE.convertVO(alarmRecord);

            AlarmEntity alarm = alarmService.getById(vo.getAlarmId());
            if (alarm != null) {
                vo.setAlarmName(alarm.getAlarmName());
            } else {
                vo.setAlarmName("已删除");
            }

            ProductEntity product = productService.getProductCacheById(vo.getProductId());
            if (product != null) {
                vo.setProductName(product.getProductName());
            } else {
                vo.setAlarmName("已删除");
            }

            DeviceEntity device = deviceService.getDeviceCacheById(vo.getDeviceId());
            if (device != null) {
                vo.setDeviceName(device.getDeviceName());
            } else {
                vo.setAlarmName("已删除");
            }

            return vo;
        }
        return null;
    }

}
